'''
FilePath: \src\data\date_loader.py
Description: 数据读取

Author: xy
Date: 2025-06-11 15:32:39
Version: 1.0
'''

# 标准库

# 第三方库
import pandas as pd

# 自定义库

class Device:
    '''设备类'''
    def __init__(self,Id ,type,name,type_id):
        self.type = type  #设备类型
        self.type_id = type_id
        self.id=0         #阶段机器id
        self.Id=Id        #设备ID
        self.name = name  #设备名称
        self.process_speed = {}  #加工时间
        self.transfertime={}

    def __str__(self):
        return f"设备Id:{self.Id},设备名字: {self.name}, 设备类型: {self.type},类型ID:{self.type_id}, 处理速度: {self.process_speed},运输时间：{self.transfertime}"


class I:
    '''订单类'''
    def __init__(self, name, num_Fur, change_time):
        self.name = name  #名字
        self.route = []  #订单路径
        self.num_Fur = num_Fur  #炉数
        self.change_time = change_time  #换包时间
        self.current_step=0

    def __str__(self):
        return f"产品类型: {self.name}, 炉数: {self.num_Fur}, 换包时间: {self.change_time}，路径：{self.route}"


class Data:
    '''数据类'''
    def __init__(self, path):
        self.excel = pd.ExcelFile(path)
        self.I = []  #订单集
        self.device = []  #设备集
        self.num_stage=0
        self.num_device = 0
        self.total_Fur = 0
        self.stage_device = []

    def get_I(self):
        '''读取订单'''
        df = self.excel.parse('01casting')
        for index, row in df.iterrows():
            name = row['钢种']
            num_Fur = row['炉数']
            change_time = row['换包时间']
            instance = I(name, num_Fur, change_time)
            self.I.append(instance)
            self.total_Fur += num_Fur
        self.get_route()

    def get_device(self):
        stage=[]
        '''读取设备'''
        df = self.excel.parse('04device')
        for index, row in df.iterrows():
            Id = index
            type=row['设备类型']
            if type not in stage:
                stage.append(type)
                self.stage_device.append([])
            name=row['设备ID']
            type_id=stage.index(type)
            self.stage_device[type_id].append(Id)
            instance = Device(Id,type, name,type_id)
            self.num_device += 1
            self.device.append(instance)
        self.get_process_speed()
        self.num_stage=len(stage)
        index=0
        for i in range(len(self.device)):
            if i !=0:
                if self.device[i].type!=self.device[i-1].type:
                    index=0
                else:
                    index+=1
            self.device[i].id=index


    def get_route(self):
        '''读取路径'''
        df = self.excel.parse('02route')
        for index, row in df.iterrows():
            name = row['钢种']
            route = row['路径']
            for i in self.I:
                if i.name == name:
                    steps = route.split("->")
                    for step in steps:
                        # 若存在 | ，说明该步骤有多个可选加工位置
                        if "|" in step:
                            i.route.append(step.split("|"))
                        else:
                            i.route.append(step)


    def get_process_speed(self):
        '''读取加工速度'''
        df = self.excel.parse('03speed')
        for index, row in df.iterrows():
            id=row['设备id']
            name=row['钢种']
            time=row['工序时间']
            for i in self.device:
                if i.name == id:
                    i.process_speed[name]=time
    
    def get_transfertime(self):
        '''读取切换时间'''
        df = self.excel.parse('05transport')
        for index, row in df.iterrows():
            name_start=row['设备ID1']
            name_end=row['设备ID2']
            time=row['时间(分钟)']
            for i in self.device:
                if i.name == name_start:
                    for j in self.device:
                        if j.name == name_end:
                            end_id=j.Id
                            i.transfertime[end_id]=time
                            break


        

    def run(self):
        self.get_I()
        self.get_device()
        self.get_transfertime()







